// import {
//   defineStore
// } from 'pinia';

// export const useSystemInfoStore = defineStore("systemInfo", () => {
//   // 使用 ref 创建响应式数据
//   const statusBarHeight = ref(uni.getStorageSync('statusBarHeight') || 0);
//   const menuButtonInfo = ref(uni.getStorageSync('menuButtonInfo') || {});
//   const navBarHeight = ref(uni.getStorageSync('navBarHeight') || 0);

//   // 如果缓存中没有数据，则进行计算并更新缓存
//   if (!navBarHeight.value) {
//     const systemInfo = uni.getSystemInfoSync(); // 使用 Sync 方法
//     statusBarHeight.value = systemInfo.statusBarHeight;
//     menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
//     navBarHeight.value = (menuButtonInfo.value.bottom - statusBarHeight.value) + (menuButtonInfo.value.top -
//       statusBarHeight.value);

//     // 更新缓存
//     uni.setStorageSync('statusBarHeight', statusBarHeight.value);
//     uni.setStorageSync('menuButtonInfo', JSON.stringify(menuButtonInfo.value)); // 菜单按钮信息需要序列化
//     uni.setStorageSync('navBarHeight', navBarHeight.value);
//   }

//   return {
//     statusBarHeight,
//     menuButtonInfo,
//     navBarHeight
//   };
// });



import {
  defineStore,

} from 'pinia';

export const useSystemInfoStore = defineStore('systemInfo', () => {
  // 初始化响应式数据
  const statusBarHeight = ref(0);
  const menuButtonInfo = ref({});
  const navBarHeight = ref(0);


  // 初始化系统信息，仅在首次访问或缓存数据无效时执行
  function initializeSystemInfo() {
    const systemInfo = uni.getSystemInfoSync();
    statusBarHeight.value = systemInfo.statusBarHeight;
    menuButtonInfo.value = uni.getMenuButtonBoundingClientRect();
    navBarHeight.value = calculateNavBarHeight(systemInfo);

    // 更新缓存
    uni.setStorageSync('statusBarHeight', statusBarHeight.value);
    uni.setStorageSync('menuButtonInfo', JSON.stringify(menuButtonInfo.value));
    uni.setStorageSync('navBarHeight', navBarHeight.value);
  }

  // 计算导航栏高度
  function calculateNavBarHeight() {
    // 这里直接使用 statusBarHeight 和 menuButtonInfo.value 计算 navBarHeight，
    // 假设 menuButtonInfo.value 已经包含了必要的信息
    // 示例逻辑可能需要根据实际情况调整
    return (menuButtonInfo.value.bottom - statusBarHeight.value) + (menuButtonInfo.value.top - statusBarHeight
      .value);
  }

  // 在 store 创建时尝试从缓存恢复数据，如果缓存不存在则初始化数据
  if (!navBarHeight.value) {
    initializeSystemInfo();
    console.log(statusBarHeight.value, "ss2")
    console.log(menuButtonInfo.value, "ss2")
    console.log(navBarHeight.value, "ss2")
  }

  return {
    statusBarHeight,
    menuButtonInfo,
    navBarHeight,
  };
});